package user

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"practice/config"
	"practice/internal/model/dao"
	"practice/tools"
)

type Registers struct {
	Mobile   string `form:"mobile" json:"mobile" xml:"mobile"  binding:"required,mobile"`
	Password string `form:"password" json:"password" xml:"password" binding:"required"`
	Name     string `form:"name" json:"name" xml:"name" binding:"required"`
	Email    string `form:"email" json:"email" xml:"email" binding:"required,email"`
}

func Register(c *gin.Context) {
	var json Registers
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	mobile := json.Mobile
	password := json.Password
	name := json.Name
	email := json.Email
	var user dao.User
	config.DB.Where("mobile=?", mobile).First(&user)
	if user.ID != 0 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "已注册过"})
		return
	}
	d := dao.User{
		Mobile:   mobile,
		Password: tools.MD5(password + "111"),
	}

	config.DB.Create(&d)
	detail := dao.UserDetail{
		UserId: int(d.ID),
		Name:   name,
		Mobile: mobile,
		Email:  email,
	}

	config.DB.Create(&detail)
	c.JSON(http.StatusOK, gin.H{
		"response": "注册成功",
		"data":     d.ID,
	})
}
